package com.car.dao.impl;

import com.car.dao.CarDao;
import com.car.domain.Car;
import com.car.utils.JDBCUtil;

import java.util.List;

/**
 * 汽车Dao的实现类
 */
public class CarDaoImpl implements CarDao {
    /**
     * 新增
     *
     * @param car 泛型
     */
    @Override
    public int add(Car car) throws Exception {
        String sql = "insert into t_car (name, brand, price, type, color, car_num, employee_id) \n" +
                "VALUE(?,?,?,?,?,?,?)";
        return JDBCUtil.update(sql, car.getName(), car.getBrand(), car.getPrice(), car.getType(),
                car.getColor(), car.getCarNum(), car.getEmployeeId());
    }

    /**
     * 删除
     *
     * @param id 主键id
     * @return 影响记录数
     */
    @Override
    public int delete(int id) throws Exception {
        String sql = "delete from t_car where id = ? ";
        return JDBCUtil.update(sql, id);
    }

    /**
     * 修改
     *
     * @param car 泛型
     */
    @Override
    public int update(Car car) throws Exception {
        String sql = "update t_car set name = ?,brand=?,price=?,type=?,color=?," +
                "car_num=?,status=?,employee_id=?\n" +
                "where id = ?";
        return JDBCUtil.update(sql, car.getName(), car.getBrand(), car.getPrice(), car.getType()
                , car.getColor(), car.getCarNum(), car.getStatus(), car.getEmployeeId(), car.getId());
    }

    /**
     * 通过主键id查询
     *
     * @param id 主键id
     * @return 实体类
     */
    @Override
    public Car queryById(int id) throws Exception {
        String sql = "select * from t_car where id = ?";
        return JDBCUtil.query(sql, Car.class, id);
    }

    /**
     * 查询所有记录
     *
     * @return 实体类集合
     */
    @Override
    public List<Car> queryAll() throws Exception {
        String sql = "select * from t_car";
        return JDBCUtil.queryList(sql, Car.class);
    }

    @Override
    public Car findByIdAndStatusEq0(int carId) throws Exception {
        String sql = "select * from t_car where status = 0 and id = ?";
        return JDBCUtil.query(sql, Car.class, carId);
    }
}
